<?php
namespace Face\Controller;
class WenController extends CommonController {
    /*软文首页*/
    public function index()
    {
        $count      = M('article')->where(['article_ischeck'=>1])->count();// 查询满足要求的总记录数
        if($count==0){
            $this->redirect('Face/Wen/kong');
        }
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('article')->where(['article_ischeck'=>1])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign(['page'=>$show,'data'=>$list]);

        $this->display();
    }
    /*软文 娱乐*/
    public function fun()
    {
        $count      = M('article')->where(['article_ischeck'=>1,'article_type'=>3])->count();// 查询满足要求的总记录数
        if($count==0){
            $this->redirect('Face/Wen/kong');
        }
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('article')->where(['article_ischeck'=>1,'article_type'=>3])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign(['page'=>$show,'data'=>$list]);

        $this->display();
    }
    /*软文 西安*/
    public function xian()
    {
        $count      = M('article')->where(['article_ischeck'=>1,'article_type'=>4])->count();// 查询满足要求的总记录数
        if($count==0){
            $this->redirect('Face/Wen/kong');
        }
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('article')->where(['article_ischeck'=>1,'article_type'=>4])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign(['page'=>$show,'data'=>$list]);

        $this->display();
    }
    /*软文 医药*/
    public function yiyao()
    {
        $count      = M('article')->where(['article_ischeck'=>1,'article_type'=>5])->count();// 查询满足要求的总记录数
        if($count==0){
            $this->redirect('Face/Wen/kong');
        }
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('article')->where(['article_ischeck'=>1,'article_type'=>5])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign(['page'=>$show,'data'=>$list]);

        $this->display();
    }
    /*软文 社会*/
    public function shehui()
    {
        $count      = M('article')->where(['article_ischeck'=>1,'article_type'=>6])->count();// 查询满足要求的总记录数
        if($count==0){
            $this->redirect('Face/Wen/kong');
        }
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('article')->where(['article_ischeck'=>1,'article_type'=>6])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign(['page'=>$show,'data'=>$list]);

        $this->display();
    }
    /*软文内容*/
    public function content()
    {
        $id = i('get.id');

        $wen = M('article')->where(['id'=>$id])->find();

        $read = $wen['article_read']+1;

        $data = [
            'article_read'=>$read
        ];

        $fankui = M('fankui')->find(1);
        $us = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
        $mon2 = $us['money']+$fankui['yuedu'];
        /*给微信用户加钱*/
        $data2 = [
            'money'=>$mon2
        ];

        $ip = $_SERVER["REMOTE_ADDR"];

        $data3 = [
            'ip'=>$ip,
            'article_id'=>$id,
            'time'=>time(),
        ];

        $data4 = [
            'openid'=>$_SESSION['wechatopenid'],
            'text'=>'阅读文章反馈 + ￥：'.$fankui['yuedu'],
            'time'=>time(),
        ];
        $dangqiandenglu = M('wechatuser')->where(['openid' => $_SESSION['wechatopenid']])->find();
        $shangjiaa = M('wechatuser')->where(['id' => $dangqiandenglu['did']])->find();
        /*阅读上家反馈*/
        $data5 = [
            'openid'=>$shangjiaa['openid'],
            'text'=>'团队成员  '.$dangqiandenglu['name'].'  阅读文章反馈 + ￥：'.$fankui['shangjia'],
            'time'=>time(),
        ];
        $mon3 = $shangjiaa['money']+$fankui['shangjia'];
        /*商家反馈金额*/
        $data6 = [
            'money'=>$mon3
        ];
        if(M('article')->where(['id'=>$id])->save($data)){
            if(M('ip')->where(['ip'=>$ip,'article_id'=>$id])->find()){
                $this->assign('data',M('article')->where(['id'=>$id])->find());
                $this->display();
            }else{
                M('ip')->add($data3);
                M('wechatuser')->where(['openid' => $_SESSION['wechatopenid']])->save($data2);
                M('fankuijilu')->add($data4);
                M('wechatuser')->where(['id' => $dangqiandenglu['did']])->save($data6);
                if(M('wechatuser')->where(['id' => $dangqiandenglu['did']])->find()){
                    M('fankuijilu')->add($data5);
                }
                $this->assign('data',M('article')->where(['id'=>$id])->find());
                $this->display();
            }
        }
    }
    /*软文为空*/
    public function kong()
    {
        $this->display();
    }
}